package com.finance.scheduler;

import com.finance.report.bean.pojo.LogisticsExtend;
import com.finance.report.bean.pojo.ManSalary;
import com.finance.report.service.ILogisticsExtendService;
import com.finance.report.service.IManSalaryService;
import com.finance.report.service.ISalaryService;
import com.finance.system.bean.pojo.Logistics;
import com.finance.util.DateUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created by CBWL on 2017/6/20.
 */
@Component
public class SelectFilter {
    @Autowired
    private IManSalaryService manSalaryService;
    @Autowired
    private ILogisticsExtendService LogisticsExtendService;

    class UserMonthIdentity{
        private String month;
        private String userCode;

        public UserMonthIdentity(String month,String userCode){
            this.month= month;
            this.userCode= userCode;
        }
        @Override
        public int hashCode(){
            return this.month.hashCode()+ this.userCode.hashCode();
        }
        @Override
        public boolean equals(Object o){
            if(!(o  instanceof UserMonthIdentity)) {
                return false;
            }
            UserMonthIdentity userMonthIdentity = (UserMonthIdentity)o;
            return this.month.equals(userMonthIdentity.month)&& this.userCode.equals(userMonthIdentity.userCode);
        }
    }

    /**
     * 过滤每次查询的结果list
     * 原则：如果该配送员该月已有ManSalary数据 则不过滤
     * 若无ManSalary 则当前list过滤后保留该配送员该月数据仅一条
     * @param list
     * @return
     */
    public List<LogisticsExtend> filter(List<LogisticsExtend> list,Date date){
        if(CollectionUtils.isEmpty(list)) {
            return list;
        }
        Map<UserMonthIdentity,List<LogisticsExtend>> map = new HashMap<UserMonthIdentity,List<LogisticsExtend>>();
        String month = this.genMonth(date);
        for(LogisticsExtend LogisticsExtend : list){
            ManSalary manSalary = this.manSalaryService.getByUserCode(LogisticsExtend.getPostmanNo(),month);
            UserMonthIdentity userMonthIdentity = new UserMonthIdentity(month,LogisticsExtend.getPostmanNo());
            if(manSalary != null){
                if(map.containsKey(userMonthIdentity)){
                    List<LogisticsExtend> sub = map.get(userMonthIdentity);
                    sub.add(LogisticsExtend);
                    map.put(userMonthIdentity,sub);
                }else{
                    List<LogisticsExtend> sub = new ArrayList<LogisticsExtend>();
                    sub.add(LogisticsExtend);
                    map.put(userMonthIdentity,sub);
                }
            }else{
                if(!map.containsKey(userMonthIdentity)){
                    List<LogisticsExtend> sub = new ArrayList<LogisticsExtend>();
                    sub.add(LogisticsExtend);
                    map.put(userMonthIdentity,sub);
                }
            }
        }
        List<LogisticsExtend> result = new ArrayList<LogisticsExtend>();
         for(List<LogisticsExtend> sublist : map.values()){
            result.addAll(sublist);
        }
        return result;
    }

    public String genMonth(Date date){
        return DateUtil.dateFormat(date,"yyyy-MM");
    }
}
